package com.scalar.chiptrack.adaptor.unisem;

import com.scalar.chiptrack.adaptor.chartered.CharteredWipStatus;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.html.parser.DocumentParser;
import javax.swing.text.MutableAttributeSet;
import java.util.ArrayList;
import java.io.FileReader;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 9 Oct, 2009
 * Time: 2:36:33 PM
 * To change this template use File | Settings | File Templates.
 */
public class UNISEMHtmlWipParser extends HTMLEditorKit.ParserCallback
{       
    private UNISEMWipStatus wipStatus = null;
    private ArrayList m_fabList = new ArrayList();
    private int m_tableTagCount; //keeps the count of number of table tags encountered
    private boolean m_readyForParsing;  //if set to true indicates that the parser has reached the data to be parsed
    private int m_cellIndex;
    private boolean m_cellOpen;
    private boolean m_invalidRow;

    public UNISEMHtmlWipParser()
    {
         m_tableTagCount = 0;
         m_readyForParsing = false;
         m_cellOpen = false;
         m_cellIndex = -1;
         m_invalidRow = false;
    }

    public ArrayList<UNISEMWipStatus> parse( FileReader reader ) throws Exception
    {
        DTD dtd = DTD.getDTD( "HTML" );
        DocumentParser documentParser = new DocumentParser( dtd );
        documentParser.parse( reader, this, true /*ignore CharSet*/ );

        for ( int i = 0; i < m_fabList.size(); i++ )
        {
            wipStatus = ( UNISEMWipStatus ) m_fabList.get( i );
        }

        return m_fabList;
    }

    public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
    {
        // FOR PARSING THE DOWNLOADED WIP STATUS FILE
        if ( t == HTML.Tag.TABLE )
        {
            m_tableTagCount++;

            //data is present in third table tag. So setting the variable to true to indicate the start of data
            if ( m_tableTagCount == 1 )
            {
                m_readyForParsing = true;
            }
            else if ( m_tableTagCount == 2 )
            {
                //this block is entered when the table containing data is parsed
                if ( ( wipStatus != null ) && ( m_invalidRow == false ) )
                {
                    m_fabList.add( wipStatus );
                }

                m_readyForParsing = false;
            }
        }

        if ( m_readyForParsing )
        {
            if ( ( t == HTML.Tag.TR ) && ( a.getAttributeCount() == 1 ) )
            {
                if ( ( wipStatus != null ) )
                {
                    if ( wipStatus.getDeviceName() != null )
                    m_fabList.add( wipStatus );
                }

                wipStatus = new UNISEMWipStatus();
                m_cellIndex = -1;
                m_invalidRow = false;
            }
        }

        // Got a new cell
        if ( m_readyForParsing )
        {
            if ( ( t == HTML.Tag.TD ) )
            {
                if ( m_cellOpen == false )
                {
                    m_cellIndex++;
                    m_cellOpen = true;
                }
                else
                {
                    m_cellOpen = false;
                }
            }
        }
    }

    public void handleText( char[] data, int pos )
    {
        if ( ( m_readyForParsing == false ) || ( wipStatus == null ) )
        {
            return;
        }

        String text = new String( data );

        if ( text == null )
        {
            text = "";
        }
        else
        {
            text = text.trim();

        }
        switch ( m_cellIndex )
        {

            case 0:

                if ( !( "".equals( text ) ) && !( "PKG".equals( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setPkgType(text);
                }
                break;

            case 1:
                if ( !( "".equals( text ) ) && !( "DEV".equals( text ) ) &&  !( text.startsWith("&nbsp") )
                        && !("SUB-TOTAL".equals( text ) ) && wipStatus.getPkgType() != null && wipStatus.getPkgType().indexOf("TOTAL") == -1 )
                {
                    wipStatus.setDeviceName( text );
                }
                break;

            case 2:
                if ( !( "".equals( text ) ) && !( "LOT.NBR".equalsIgnoreCase( text ) ) &&  !( text.startsWith("&nbsp") )
                        && wipStatus.getDeviceName() != null && wipStatus.getDeviceName().indexOf("TOTAL") == -1 )
                {
                    wipStatus.setLotNumber( text );
                }
                break;

            case 3:
                if ( !( "".equals( text ) ) && !( "INV.QTY".equalsIgnoreCase( text.trim() ) )  &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setInvQty( text );
                }
                break;

            case 4:
                if ( !( "".equals( text ) ) && !( "D/B.".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") )  )
                {
                    wipStatus.setDieBankQty( text );
                }
                break;

            case 5:
                if ( !( "".equals( text ) ) && !( "R/C".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
                {
                    wipStatus.setRcQty( text );
                }
                break;

            case 6:
               if ( !( "".equals( text ) ) && !( "D/S.".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
               {
                   wipStatus.setDsQty( text );
               }
               break;

            case 7:
                if ( !( "".equals( text ) ) && !( "D/P.".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setDpQty( text );
                }
                break;

            case 8:
                if ( !( "".equals( text ) ) && !( "D/A.".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setDieAttach( text );
                }
                break;

            case 9:
                if ( !( "".equals( text ) ) && !( "W/B.".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setWireBond( text );
                }
                break;

            case 10:
                if ( !( "".equals( text ) ) && !( "COAT".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setCoating( text );
                }
                break;

            case 11:
                if ( !( "".equals( text ) ) && !( "MOLD".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setMolding( text );
                }
                break;

            case 12:
                if ( !( "".equals( text ) ) && !( "DEJK".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setDejk( text );
                }
                break;

            case 13:
                if ( !( "".equals( text ) ) && !( "SLUR".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setSluring( text );
                }
                break;

            case 14:
                if ( !( "".equals( text ) ) && !( "MARK".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setMarking( text );
                }
                break;

            case 15:
                if ( !( "".equals( text ) ) && !( "M/F.".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setMfQty( text );
                }
                break;

            case 16:
                if ( !( "".equals( text ) ) && !( "T/F.".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setTfQty( text );
                }
                break;

            case 17:
                if ( !( "".equals( text ) ) && !( "F/V.".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setFvQty( text );
                }
                break;

            case 18:
                if ( !( "".equals( text ) ) && !( "F/T.".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setFinalTest( text );
                }
                break;

            case 19:
                if ( !( "".equals( text ) ) && !( "F/G".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setFgQty( text );
                }
                break;

            //case 20:
            //case 21:

            case 22:
                if ( !( "".equals( text ) ) && !( "RCVD.".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setRecvDate( text );
                }
                break;

            case 23:
                if ( !( "".equals( text ) ) && !( "IQC.D".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setIqcDate( text );
                }
                break;
            case 25:
                if ( !( "".equals( text ) ) && !( "ASY OUT".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setIqcDate( text );
                }
                break;
            //24,25,26,27,28
            case 29:
                if ( !( "".equals( text ) ) && !( "PO#".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setPoNumber( text );
                }
                break;
           default:
                break;
        }

    }

    // Resets the objects state
    public void reset()
    {
        m_tableTagCount = 0;
        m_readyForParsing = false;
        m_cellOpen = false;
        m_cellIndex = -1;
        m_invalidRow = false;
    }

}

